package com.yjc.hire.controller;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.yjc.bo.SearchBO;
import com.yjc.hire.VO.UsersVO;
import com.yjc.hire.base.BaseInfoProperties;
import com.yjc.hire.bo.ModifyUserBO;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.service.UserService;
import com.yjc.hire.utils.JWTUtils;
import com.yjc.pojo.Users;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/7/20
 * </p>
 */

@RestController
@RequestMapping("userinfo")
@Slf4j
public class UserInfoController {

    @Autowired
    private UserService userService;

    @Autowired
    private JWTUtils jwtUtils;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private final static String REDIS_COMPANY_HR_COUNT = "redis_company_hr_count";

    /**
     * 修改用户信息
     * @param modifyUserBO
     * @return
     */
    @PostMapping("modify")
    public GraceJSONResult modify(@Valid @RequestBody ModifyUserBO modifyUserBO) {
        userService.modifyUserInfo(modifyUserBO);
        //返回最新用户信息进行刷新
        UsersVO newUser = getUsersInfo(modifyUserBO.getUserId());
        return GraceJSONResult.ok(newUser);
    }

    private UsersVO getUsersInfo(String userId){
        Users latestUser = userService.selectUserInfoById(userId);
        String uToken = jwtUtils.createJWtWithPrefix(new Gson().toJson(latestUser), BaseInfoProperties.TOKEN_USER_PREFIX);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(latestUser, usersVO);
        usersVO.setUserToken(uToken);
        return usersVO;
    }

    @PostMapping("getCountByCompanyId")
    public GraceJSONResult getCountByCompanyId(@RequestParam("companyId") String companyId){
        String key = REDIS_COMPANY_HR_COUNT + ":" + companyId;
        String hrCountHr = stringRedisTemplate.opsForValue().get(key);
        Long hrCount = 0l;
        if (StringUtils.isBlank(hrCountHr)){
             hrCount = userService.getHrCountByCompanyId(companyId);
             stringRedisTemplate.opsForValue().set(key, String.valueOf(hrCount),5 * 60);
             //FIXME: 此处有缓存击穿的问题
        }else {
            hrCount = Long.parseLong(hrCountHr);
        }
        return GraceJSONResult.ok(hrCount);
    }

    @PostMapping("bindingHRToCompany")
    public GraceJSONResult bindingHRToCompany(@RequestParam("hrUserId") String hrUserId, @RequestParam("companyId") String companyId
    ,@RequestParam("realName") String realName){
        userService.updateUserCompanyId(hrUserId,companyId,realName);
        Users hrUser = userService.getByUserId(hrUserId);
        return GraceJSONResult.ok(hrUser.getMobile());
    }

    /**
     * s刷新用户信息
     * @param userId
     * @return
     */
    @PostMapping("freshUserinfo")
    public GraceJSONResult freshUserinfo(@RequestParam("userId") String userId){
        UsersVO usersInfo = getUsersInfo(userId);
        return GraceJSONResult.ok(usersInfo);
    }

    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @PostMapping("get")
    public GraceJSONResult get(@RequestParam("userId") String userId){
        UsersVO usersInfo = getUsersInfo(userId);
        usersInfo.setUserToken("");
        return GraceJSONResult.ok(usersInfo);
    }

    @PostMapping("changeUserToHr")
    public GraceJSONResult changeUserToHr(@RequestParam("userId") String userId){
        userService.updateUserToHR(userId);
        return GraceJSONResult.ok();
    }

    /**
     * 查询下属企业的所有hr列表
     * @param limit
     * @param page
     * @return
     */
    @PostMapping("saas/hrList")
    public GraceJSONResult saasHrList(Integer limit,Integer page,String companyId){
        if (page == null) page=1;
        if (limit == null) limit = 10;
        if (StringUtils.isEmpty(companyId)) return GraceJSONResult.error();
        PageGridResult<List<Users>> hrList = userService.getHrListByCompanyId(companyId,page,limit);
        return GraceJSONResult.ok(hrList);
    }

    @PostMapping("changeUserToCand")
    public GraceJSONResult changeUserToCand(@RequestParam("userId") String userId){
        userService.updateUserToCand(userId);
        return GraceJSONResult.ok();
    }
    @PostMapping("list/get")
    public GraceJSONResult getList(@RequestBody SearchBO searchBO){
        List<Users> usersList = userService.getList(searchBO);
        List<UsersVO> usersVOList = new ArrayList<>();
        for (Users users : usersList) {
            UsersVO vo = new UsersVO();
            BeanUtils.copyProperties(users, vo);
            usersVOList.add(vo);
        }
        String json = new Gson().toJson(usersVOList);
        return GraceJSONResult.ok(json);
    }
}
